/*
 *   SlimPlayer Copyright (c) 2006 Koelec
 *
 *   This file is part of SlimPlayer plugin for SageTv.
 *
 *   Slimplayer is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   Slimplayer is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with SoftSqueeze; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

package nl.cmkprojects.sage.web;

import java.io.FileNotFoundException;
import java.net.URLEncoder;

import org.apache.log4j.Logger;
import org.webharvest.definition.ScraperConfiguration;
import org.webharvest.runtime.Scraper;
import org.webharvest.runtime.variables.Variable;

/**
 * @author Chris
 * 
 *         To change the template for this generated type comment go to Window -
 *         Preferences - Java - Code Generation - Code and Comments
 */
public class WebHarvestArtistInfoRetriever implements ArtistInfoRetriever {
	public static Logger logger = Logger
	.getLogger(WebHarvestArtistInfoRetriever.class);

	public static void main(String[] args) {
		ArtistInfoRetriever air;
		try {
			air = new WebHarvestArtistInfoRetriever();
			air.getInfo("howard stern");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public WebHarvestArtistInfoRetriever() throws FileNotFoundException {
		// TODO Auto-generated constructor stub
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * nl.cmkprojects.sage.webradio.ArtistInfoRetriever#getInfo(java.lang.String
	 * )
	 */
	public ArtistInfo getInfo(String artistName) {
		logger.debug("InfoRetriever(" + artistName + ")");
		ArtistInfo ai = new ArtistInfo();
		ai.setName(artistName);
		ai.setPictureURL("");
		ai.setBio("");

		Scraper scraper = null;
		try {
			ScraperConfiguration config = new ScraperConfiguration(
					"slimplayer/slimplayer-ai.xml");
			scraper = new Scraper(config, ".");
	    String searchName = URLEncoder.encode(artistName, "UTF-8");
			scraper.addVariableToContext("artist", searchName);

			if (logger.isDebugEnabled()) {
				scraper.setDebug(true);
			}

			scraper.execute();

			// takes variable created during execution
			Variable imgUrlVar = (Variable) scraper.getContext().get("picUrl");
			Variable bioVar = (Variable) scraper.getContext().get("bio");
			Variable photosVar = (Variable) scraper.getContext().get(
					"photoUrls");
			ai.setPictureURL(imgUrlVar.toString());
			ai.setBio(InfoFormatter.formatText(bioVar.toString(), "\n"));
			String imgUrl = imgUrlVar.toString();
			if (imgUrl != null && imgUrl.length() > 0) {
				ai.getPhotos().add(imgUrl);
			}
			for (Object o : photosVar.toList()) {
				ai.getPhotos().add(o.toString());
			}
			logger.debug(ai);
		} catch (Exception e) {
			logger.debug("failed to run scraper", e);
		} finally {
			if (scraper != null) {
				scraper.dispose();
			}
		}
		return ai;
	}
}
